﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using BUL;
using DAL;
using QLCongNo.KhachHang;
using DevExpress.XtraGrid;
using System.Xml;

namespace QLCongNo
{
    public partial class DSKhachHangUser : UserControl
    {
        KhachhangBUL kh = new KhachhangBUL();
        KhachhangDAL khachhang = new KhachhangDAL();
        public DSKhachHangUser()
        {
            InitializeComponent();
        }

        private void UserControl1_Load(object sender, EventArgs e)
        {

            DataTable tb = new DataTable();
            tb = kh.DanhsachKH();
            loadGridView(tb);
            lb_SoKhachHang.Text = lb_SoKhachHang.Text + ": " + tb.Rows.Count.ToString();
       
        }

        private void themKhachHangbtn_Click(object sender, EventArgs e)
        {
            ThemKhachHangform them = new ThemKhachHangform();
            them.ShowDialog();
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            int i=khachhang2.CurrentCell.RowIndex;
            string ma=khachhang2.Rows[i].Cells[0].Value.ToString();
            string ten = khachhang2.Rows[i].Cells[1].Value.ToString();
            if (MessageBox.Show("Có chắc muốn xóa "+ten+" khỏi danh sách", "Xóa khách hàng", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                string sql = "DELETE FROM KhachHang WHERE MaKH='"+ma+"'";
                KhachhangBUL kh = new KhachhangBUL();
                Boolean kq =kh.XoaKH(sql);
                if (kq == true)
                {
                    MessageBox.Show("Xóa thành công"+ten);
                }
                else
                    MessageBox.Show("Lỗi");
                this.DSKH();
            }


        }

        public void DSKH() {
            DataTable tb = new DataTable();
            
            tb = kh.DanhsachKH();
            khachhang2.DataSource = tb;
        }

        private void txt_timkiem_EditValueChanged(object sender, EventArgs e)
        {
            DataTable tb = new DataTable();

            tb = khachhang.TimKiemKH(txt_timkiem.Text.ToString());
            khachhang2.DataSource = tb;
        }



        private void khachhang2_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            int i = khachhang2.CurrentCell.RowIndex;
            string ma = khachhang2.Rows[i].Cells[0].Value.ToString();
            MessageBox.Show(ma);
        }

        private void panelControl1_Paint(object sender, PaintEventArgs e)
        {

        }
        public void loadGridView(DataTable ds)
        {
            khachhang2.DataSource = ds;
            khachhang2.Columns[0].Width = 150;
            khachhang2.Columns[1].Width = 150;
            khachhang2.Columns[2].Width = 150;
            khachhang2.Columns[3].Width = 150;
            khachhang2.Columns[4].Width = 250;
            khachhang2.Columns[5].Width = 200;
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            
        }





            
    }
}
